package com.css.fxfzaqbz.modules.base.service.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess1.entity.AlgEntity;
import com.css.fxfzaqbz.modules.assess1.repository.DestoryAlgRepository;
import com.css.fxfzaqbz.modules.base.entity.District;
import com.css.fxfzaqbz.modules.base.entity.SeismicResult;
import com.css.fxfzaqbz.modules.base.queryParams.SeismicQueryParam;
import com.css.fxfzaqbz.modules.base.repository.SeismicResultRepository;
import com.css.fxfzaqbz.modules.base.service.SeismicResultService;
import com.css.fxfzaqbz.modules.supergraph.repository.OrgDivisionRepository;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformSessionUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class SeismicResultServiceImpl implements SeismicResultService {
    @Resource
    SeismicResultRepository seismicResultRepository;

    @Resource
    DestoryAlgRepository destoryAlgRepository;
    @Resource
    OrgDivisionRepository orgDivisionRepository;
    @Override
    public void save(SeismicQueryParam param, District district, BigDecimal result) {
        SeismicResult seismicResult = new SeismicResult();
        seismicResult.setId(UUIDGenerator.getUUID());
        seismicResult.setIaId(param.getIaId());
        seismicResult.setLieDu(param.getLieDu());
        seismicResult.setCity(district.getCity());
        seismicResult.setProvince(district.getProvince());
        seismicResult.setPrefecture(district.getPrefecture());
        seismicResult.setDelFlag(YNEnum.N.toString());
        seismicResult.setRes(result);
        seismicResult.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        seismicResult.setCreateUser(PlatformSessionUtils.getUserId());

        String district1=null;
        //北京市，上海市，天津市，重庆市
        if ("北京市".equals(district.getProvince())||"上海市".equals(district.getProvince())||"天津市".equals(district.getProvince())||"重庆市".equals(district.getProvince())){
            district1=district.getCity()+"-"+district.getPrefecture();
        }else {
            district1=district.getProvince()+"-"+district.getCity()+"-"+district.getPrefecture();
        }
        String code = orgDivisionRepository.getCodeByTaskDistrict(district1);

        String geom=destoryAlgRepository.getGeomBySSQ(code);
        seismicResult.setGeom(geom);
        seismicResultRepository.save(seismicResult);
    }

    @Override
    public void delete(String iaId) {
        seismicResultRepository.delete(iaId);
    }

    @Override
    public void saveResult(SeismicResult seismicResult) {
        seismicResult.setId(UUIDGenerator.getUUID());
        seismicResult.setDelFlag(YNEnum.N.toString());
        seismicResult.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        seismicResult.setCreateUser(PlatformSessionUtils.getUserId());
        String district1=null;
        //北京市，上海市，天津市，重庆市
        if ("北京市".equals(seismicResult.getProvince())||"上海市".equals(seismicResult.getProvince())||"天津市".equals(seismicResult.getProvince())||"重庆市".equals(seismicResult.getProvince())){
            district1=seismicResult.getCity()+"-"+seismicResult.getPrefecture();
        }else {
            district1=seismicResult.getProvince()+"-"+seismicResult.getCity()+"-"+seismicResult.getPrefecture();
        }
        String code = orgDivisionRepository.getCodeByTaskDistrict(district1);

        String geom=destoryAlgRepository.getGeomBySSQ(code);
        seismicResult.setGeom(geom);
        seismicResultRepository.save(seismicResult);
    }

    @Override
    public List<SeismicResult> getById(String id) {
        return seismicResultRepository.getById(id);
    }

    @Override
    public List<AlgEntity> getData(String id) {
        return seismicResultRepository.getData(id);
    }
}
